package com.wuming.caiwu.service.impl;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.WumingErrorCode;
import com.wuming.caiwu.dao.QsgTeachingAssistantMapper;
import com.wuming.caiwu.model.QsgTeachingAssistant;
import com.wuming.caiwu.service.QsgTeachingAssistantService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("teachingAssistantService")
public class QsgTeachingAssistantServiceImpl extends BaseServiceImpl implements QsgTeachingAssistantService {

    @Autowired
    private QsgTeachingAssistantMapper teachingAssistantMapper;

    @Override
    public void saveQsgTeachingAssistant(QsgTeachingAssistant assistant) throws BusinessException {
        assistant.setCreateDatetime(new Date());
        teachingAssistantMapper.insert(assistant);
    }

    @Override
    public void updateQsgTeachingAssistant(QsgTeachingAssistant assistant) throws BusinessException {
        teachingAssistantMapper.updateByPrimaryKey(assistant);
    }

    @Override
    public List<QsgTeachingAssistant> getTeachingAssistantByPhone(String phone) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(phone)){
            params.put("phone",phone);
        }
        return teachingAssistantMapper.selectByParams(params);
    }

    @Override
    public List<QsgTeachingAssistant> getTeachingAssistantByPage(String nickname, String teachLevel, int pageIndex, int pageSize) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(nickname)){
            params.put("nickname",nickname);
        }
        if(StringUtils.isNotBlank(teachLevel)){
            params.put("teachLevel",teachLevel);
        }
        try{
            return (List<QsgTeachingAssistant>) this.getPageList(QsgTeachingAssistantMapper.class, "selectByParams", params, pageIndex, pageSize);
        }catch(Exception e) {
            logger.error("查询助教信息失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public int getTotal(String nickname, String teachLevel) throws BusinessException {
        Map<String,Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(nickname)){
            params.put("nickname",nickname);
        }
        if(StringUtils.isNotBlank(teachLevel)){
            params.put("teachLevel",teachLevel);
        }
        try{
            return this.getTotal(QsgTeachingAssistantMapper.class, "selectTotal", params);
        }catch(Exception e) {
            logger.error("查询助教总数失败{}" + e.getMessage());
            throw new BusinessException(WumingErrorCode.HUIQIAN_USERCENTER_DATABASE_OPERATE_ERROR);
        }
    }

    @Override
    public QsgTeachingAssistant getTeachingAssistantById(Long id) throws BusinessException {
        return teachingAssistantMapper.selectByPrimaryKey(id);
    }

    @Override
    public void removeTeachingAssistantById(Long id) throws BusinessException {
        teachingAssistantMapper.deleteByPrimaryKey(id);
    }
}
